//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ehl.capture.db;

import com.ehl.capture.tool.ReflectUtil;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

public class DBProxyBean {
    private static Logger logger = Logger.getLogger(DBProxyBean.class);

    public DBProxyBean() {
    }

    public static Object queryObject(String sql, Class clazz, String datasource) {
        DBProxy.printSql(sql);
        Connection con = null;
        Object obj = null;

        try {
            if (datasource != null && !"".equals(datasource)) {
                con = DBPool.getConnection(datasource);
            } else {
                con = DBPool.getConnection();
            }

            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            obj = queryLineResult(sql, clazz, con);
            if (con != null) {
                con.close();
            }
        } catch (Exception var13) {
            logger.error("根据sql获取对象失败:" + sql, var13);
            throw new RuntimeException(var13.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception var12) {
                throw new RuntimeException(var12.getMessage());
            }

        }

        return obj;
    }

    public static List<Object> queryList(String sql, Class clazz, String datasource) {
        DBProxy.printSql(sql);
        Connection con = null;
        new ArrayList();

        List objList;
        try {
            if (datasource != null && !"".equals(datasource)) {
                con = DBPool.getConnection(datasource);
            } else {
                con = DBPool.getConnection();
            }

            if (con == null) {
                throw new RuntimeException("数据库连接失败");
            }

            objList = queryMultiResult(sql, clazz, con);
            if (con != null) {
                con.close();
            }
        } catch (Exception var13) {
            logger.error("根据sql获取列表失败:" + sql, var13);
            throw new RuntimeException(var13.getMessage());
        } finally {
            try {
                if (con != null) {
                    con.close();
                }
            } catch (Exception var12) {
                throw new RuntimeException(var12.getMessage());
            }

        }

        return objList;
    }

    private static Object queryLineResult(String sql, Class clazz, Connection con) {
        Statement stat = null;
        ResultSet rs = null;
        Object obj = null;
        if (con == null) {
            return null;
        } else {
            try {
                stat = con.createStatement(1004, 1007);
                rs = stat.executeQuery(sql);
                if (rs != null && rs.next()) {
                    obj = ReflectUtil.reflectObject(clazz, rs);
                }

                if (rs != null) {
                    rs.close();
                }

                if (stat != null) {
                    stat.close();
                }
            } catch (Exception var14) {
                logger.error("根据sql获取对象失败:" + sql, var14);
                throw new RuntimeException(var14.getMessage());
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }

                    if (stat != null) {
                        stat.close();
                    }
                } catch (Exception var13) {
                    var13.printStackTrace();
                }

            }

            return obj;
        }
    }

    private static List<Object> queryMultiResult(String sql, Class clazz, Connection con) {
        Statement stat = null;
        ResultSet rs = null;
        List<Object> objList = new ArrayList();
        if (con == null) {
            return null;
        } else {
            try {
                stat = con.createStatement(1004, 1007);
                rs = stat.executeQuery(sql);
                if (rs != null) {
                    while(rs.next()) {
                        Object obj = ReflectUtil.reflectObject(clazz, rs);
                        objList.add(obj);
                    }
                }

                if (rs != null) {
                    rs.close();
                }

                if (stat != null) {
                    stat.close();
                }
            } catch (Exception var14) {
                logger.error("根据sql获取列表失败:" + sql, var14);
                throw new RuntimeException(var14.getMessage());
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }

                    if (stat != null) {
                        stat.close();
                    }
                } catch (Exception var13) {
                    var13.printStackTrace();
                }

            }

            return objList;
        }
    }
}
